package com.sxtyxt.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.sxtyxt.widget.IosAlertDialog;
import com.sxtyxt.widget.MyDatePickerDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public final class DialogUtil {

    private DialogUtil() {
    }

    public static interface ConfirmListener {
        public void yes();

        public void no();
    }

    public static interface PromptListener {
        public void yes(String value);

        public void no();
    }

    public static interface SelectItemListener {
        public void select(int index, String value);

        public void no();
    }

    public static void confirm(Context context, int messageResId, final ConfirmListener listener) {
        confirm(context, context.getApplicationContext().getResources().getString(messageResId), listener);
    }

    public static void confirm(Context context, String message, final ConfirmListener listener) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setMessage(message).setPositiveButton("是", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.yes();
            }
        }).setCancelable(true).setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                listener.no();
            }
        }).setNegativeButton("否", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.no();
            }
        }).setTitle("系统提示");

        dialogBuilder.setIcon(android.R.drawable.ic_dialog_info);

        dialogBuilder.create().show();
    }

    public static void select(Context context, String title, int defaultItem, int itemsId,
                              final SelectItemListener listener) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        final String[] arrays = context.getApplicationContext().getResources().getStringArray(itemsId);
        dialogBuilder.setSingleChoiceItems(arrays, defaultItem, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String value = arrays[which];
                if (listener != null) {
                    listener.select(which, value);
                }
                dialog.dismiss();
            }
        }).setCancelable(true).setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                if (listener != null) {
                    listener.no();
                }
            }
        }).setTitle(title);

        dialogBuilder.setIcon(android.R.drawable.ic_dialog_info);

        dialogBuilder.create().show();
    }

    public static void select(Context context, String title, int defaultItem, String[] items,
                              final SelectItemListener listener) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        final String[] arrays = items;
        dialogBuilder.setSingleChoiceItems(arrays, defaultItem, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String value = arrays[which];
                if (listener != null) {
                    listener.select(which, value);
                }
                dialog.dismiss();
            }
        }).setCancelable(true).setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                if (listener != null) {
                    listener.no();
                }
            }
        }).setTitle(title);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_info);
        dialogBuilder.create().show();
    }

    public static interface SelectDateListener {
        public void select(String year, String month, String day, String dateString);

    }

    public static interface SelectDateAndTimeListener {
        public void select(String year, String month, String day, String hourString, String minuteString);
    }

    public static interface SelectTimeListener {
        public void select(int hour, int minute, String timeString);

    }

    public static interface MultiSelectListener {
        public void select(List<String> result, List<Integer> indexs);

    }

    public static void selectDate(Context context, final SelectDateListener listener) {
        Calendar c = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(context, new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                String yearString = year + "";
                String monthString = (monthOfYear + 1) + "";
                String dayString = dayOfMonth + "";
                if (monthOfYear < 9) {
                    monthString = "0" + (monthOfYear + 1);
                }
                if (dayOfMonth < 10) {
                    dayString = "0" + dayOfMonth;
                }

                listener.select(yearString, monthString, dayString, yearString + "-" + monthString + "-" + dayString);
            }
        }, c.get(Calendar.YEAR), // 传入年份
                c.get(Calendar.MONTH), // 传入月份
                c.get(Calendar.DAY_OF_MONTH) // 传入天数
        );
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    private static boolean isfirst;

    public static void selectTime(final Context context, final SelectDateAndTimeListener listener) {
        isfirst = true;
        final Calendar c = Calendar.getInstance();
        MyDatePickerDialog dialog = new MyDatePickerDialog(context, new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {

                if (!isfirst)
                    return;
                isfirst = false;

                final String yearString = year + "";
                final String monthString = monthOfYear < 9 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                final String dayString = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";

                TimePickerDialog timedialog = new TimePickerDialog(context, new OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker arg0, int hour, int minute) {
                        String hourString = hour + "";
                        String minuteString = minute + "";
                        if (hour < 10) {
                            hourString = "0" + hour;
                        }
                        if (minute < 10) {
                            minuteString = "0" + minute;
                        }

                        listener.select(yearString, monthString, dayString, hourString, minuteString);

                    }

                }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
                timedialog.setCancelable(true);
                timedialog.setCanceledOnTouchOutside(true);
                timedialog.show();

            }
        }, c.get(Calendar.YEAR), // 传入年份
                c.get(Calendar.MONTH), // 传入月份
                c.get(Calendar.DAY_OF_MONTH) // 传入天数
        );
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    public static void selectTimeOnly(final Context context, final SelectTimeListener listener) {
        final Calendar c = Calendar.getInstance();

        TimePickerDialog timedialog = new TimePickerDialog(context, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
                String hourString = arg1 + "";
                String minuteString = arg2 + "";
                if (arg1 < 10) {
                    hourString = "0" + arg1;
                }
                if (arg2 < 10) {
                    minuteString = "0" + arg2;
                }
                listener.select(arg1, arg2, hourString + ":" + minuteString);

            }
        }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
        timedialog.setCancelable(true);
        timedialog.setCanceledOnTouchOutside(true);
        timedialog.show();

    }

    public static void muilSelect(Context context, String title, String[] items, boolean[] checkedItems,
                                  final MultiSelectListener listener) {
        final List<Integer> indexList = new ArrayList<Integer>();
        final List<String> strList = new ArrayList<String>();
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        final String[] arrays = items;

        for (int i = 0; i < checkedItems.length; i++) {
            if (checkedItems[i]) {
                strList.add(arrays[i]);
                indexList.add(i);
            }
        }
        dialogBuilder.setMultiChoiceItems(arrays, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    strList.add(arrays[which]);
                    indexList.add(which);
                } else {
                    strList.remove(arrays[which]);
                    indexList.remove(new Integer(which));
                }
            }
        }).setTitle(title);
        dialogBuilder.setPositiveButton("确定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.select(strList, indexList);
            }
        });
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_info);
        dialogBuilder.create().show();

    }

    /**
     * 仿ios7对话框 yesbutton 和nobutton不想显示直接赋值null即可，
     *
     * @param title
     * @param message        isShowMessge==false 传空即可
     * @param yesButton
     * @param noButton
     * @param isShowEditText 设置是否显示文本框edittext 想获取文本框的值调用dialog.getEditTextContent()
     * @param isShowMessge   设置是否显示文字，默认显示
     * @param listener       两个按钮的点击事件
     * @return IosAlertDialog
     */
    public static IosAlertDialog showDialog(Context context, String title, String message, String yesButton,
                                            String noButton, boolean isShowEditText, boolean isShowMessge, final DialogSelectListener listener) {

        final IosAlertDialog dialog = new IosAlertDialog(context).builder();
        dialog.setTitle(title);
        dialog.setMsg(message);
        dialog.setCancelable(true);
        dialog.setIsShowEditText(isShowEditText);
        dialog.setIsShowMsg(isShowMessge);

        if (noButton != null) {
            dialog.setNegativeButton(noButton, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.no();
                }
            });
        }
        if (yesButton != null) {
            dialog.setPositiveButton(yesButton, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.yes(dialog.getEditTextContent());
                }
            });
        }

        dialog.show();
        return dialog;
    }

    /**
     * 仿ios7对话框 yesbutton 和nobutton不想显示直接赋值null即可，
     *
     * @param titleResId
     * @param yesButton
     * @param noButton
     * @param isShowEditText 设置是否显示文本框edittext 想获取文本框的值调用dialog.getEditTextContent()
     * @param isShowMessge   设置是否显示文字，默认显示
     * @param listener       两个按钮的点击事件
     * @return IosAlertDialog
     */
    public static IosAlertDialog showIosDialog(Activity act, int titleResId, String yesButton, String noButton,
                                               boolean isShowEditText, boolean isShowMessge, final DialogSelectListener listener) {

        final IosAlertDialog dialog = new IosAlertDialog(act).builder();
        dialog.setTitle("系统提示");
        dialog.setMsg(titleResId);
        dialog.setCancelable(true);
        dialog.setIsShowEditText(isShowEditText);
        dialog.setIsShowMsg(isShowMessge);
        if (noButton != null) {
            dialog.setNegativeButton(noButton, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.no();
                }
            });
        }
        if (yesButton != null) {
            dialog.setPositiveButton(yesButton, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.yes(dialog.getEditTextContent());
                }
            });
        }

        dialog.show();
        return dialog;
    }

    /**
     * 仿ios7对话框 yesbutton 和nobutton不想显示直接赋值null即可
     *
     * @param act
     * @param title
     * @param message
     * @param yesButton
     * @param noButton
     * @param isShowEditText
     * @param isShowMessge
     * @param listener
     * @return
     */
    public static IosAlertDialog showIosDialog(Activity act, String title, String message, String yesButton,
                                               String noButton, boolean isShowEditText, boolean isShowMessge, final DialogSelectListener listener) {

        final IosAlertDialog dialog = new IosAlertDialog(act).builder();
        dialog.setTitle(title);
        dialog.setMsg(message);
        dialog.setCancelable(true);
        dialog.setIsShowEditText(isShowEditText);
        dialog.setIsShowMsg(isShowMessge);
        if (noButton != null) {
            dialog.setNegativeButton(noButton, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.no();
                }
            });
        }
        if (yesButton != null) {
            dialog.setPositiveButton(yesButton, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.yes(dialog.getEditTextContent());
                }
            });
        }

        dialog.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                listener.onDismiss();
            }
        });

        dialog.show();
        return dialog;
    }

    public interface DialogSelectListener {

        void yes(String editTextContent);

        /**
         *
         */
        void onDismiss();

        void no();

    }

    public interface DialogAlertListener {

        void yes();

    }

    public static void alertIosDialog(Activity act, String title, String message) {

        IosAlertDialog dialog = new IosAlertDialog(act).builder();
        dialog.setTitle(title);
        dialog.setMsg(message);
        dialog.setPositiveButton("确定", null);
        dialog.show();
    }

    public static void alertIosDialog(Activity act, String title, String message, final DialogAlertListener listener) {

        IosAlertDialog dialog = new IosAlertDialog(act).builder();
        dialog.setTitle(title);
        dialog.setMsg(message);
        dialog.setPositiveButton("确定", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.yes();
            }
        });
        dialog.setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });
        if (act != null && !act.isFinishing()) {
            dialog.show();
        }
    }

    /**
     * @param act
     * @param title
     * @param message
     * @param yesButton
     * @param noButton
     * @param listener
     */
    public static void confirmIosDialog(Activity act, String title, String message, String yesButton, String noButton,
                                        final DialogSelectListener listener) {

        IosAlertDialog dialog = new IosAlertDialog(act).builder();
        dialog.setTitle(title);
        dialog.setMsg(message);
        dialog.setCancelable(false);
        dialog.setPositiveButton(yesButton == null ? "确定" : yesButton, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.yes(null);
            }
        });
        dialog.setNegativeButton(noButton == null ? "取消" : noButton, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.no();
            }
        });
        dialog.show();
    }

    /**
     * @param title
     * @param message
     * @param defaultValue
     * @param listener
     * @return
     */
    public static IosAlertDialog promptIosDialog(Activity act, String message, String defaultValue,
                                                 final DialogSelectListener listener) {

        final IosAlertDialog dialog = new IosAlertDialog(act).builder();
        dialog.setEditTextContent(defaultValue);
        dialog.setCancelable(true);
        dialog.setIsShowEditText(true);
        dialog.setIsShowMsg(false);
        dialog.setPositiveButton("确定", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.yes(dialog.getEditTextContent());

            }
        });
        dialog.setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.no();
            }
        });

        dialog.show();
        return dialog;
    }


}
